package edu.yuhf.service;

import com.yuhf.dbutils.TranscationManager;
import com.yuhf.dbutils.exception.AccessDbException;
import com.yuhf.dbutils.exception.TranscationException;
import edu.yuhf.dao.AchievementJdbcDaoImpl;
import edu.yuhf.dao.StudentJdbcDaoImpl;
import edu.yuhf.dao.iface.AchievementDao;
import edu.yuhf.dao.iface.StudentDao;
import edu.yuhf.service.iface.StudentService;
import lombok.extern.log4j.Log4j;

@Log4j
public class StudentServiceImpl implements StudentService {

    private StudentDao studentDao = StudentJdbcDaoImpl.factory();
    private AchievementDao achievementDao = AchievementJdbcDaoImpl.factory();

    @Override
    public boolean deleteStudent(int id) {
        boolean flag = false;
        try {
            TranscationManager.beginTranscation();
            achievementDao.deleteStudentAchievement(id);
            int totalRow = studentDao.deleteStudent(id);
            if(totalRow==1){
                flag=true;
            }
            TranscationManager.commit();
        } catch (TranscationException e) {
            TranscationManager.rollback();
            log.error("delete student error,message is "+e.getMessage());
            throw new AccessDbException("学生删除失败");
        }
        return flag;
    }
}
